/* 
 * 
 * Created on 14 Oct 2011 by simonk
 * 
 * Copyright: Multicom Products Ltd. 2011 Bristol, England
 */
package utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.DecimalFormat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import objects.data.WorkPacket;
import objects.data.ClusterData;
import objects.info.UserInfo;

/**
 * 
 * @author simonk
 */
public class Utils {
	public static final String VERSION = "1.0";
	public static ExecutorService exec = Executors.newCachedThreadPool();

	public static final boolean DEBUG = true;

	public static UserInfo userInfo;
	public static String username;
	public static String password;

	public static void out(Object o) {
		if (DEBUG) {
			System.out.println(o.toString());
		}
	}

	public static String getMd5Digest(String pInput) {
		try {
			MessageDigest lDigest = MessageDigest.getInstance("MD5");
			lDigest.update(pInput.getBytes());
			BigInteger lHashInt = new BigInteger(1, lDigest.digest());
			return String.format("%1$032X", lHashInt);
		} catch (NoSuchAlgorithmException lException) {
			throw new RuntimeException(lException);
		}
	}

	public static String generateRandomString() {
		char[] chars = "abcdefghijklmnopqrstuvwxyzABSDEFGHIJKLMNOPQRSTUVWXYZ1234567890".toCharArray();
		SecureRandom r = new SecureRandom();
		char[] id = new char[8];
		for (int i = 0;  i < 8;  i++) {
		    id[i] = chars[r.nextInt(chars.length)];
		}
		return new String(id);
	}
	
	public static String readableFileSize(long size) {
	    if(size <= 0) return "0";
	    final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
	    int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
	    return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
	}
}
